﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SadlandVillage.Core;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using SadlandVillage.Core.Helper;

namespace SadlandVillage
{
    public class Calibration : GameScene
    {
        bool calibrationStarted = false;
        Crosshair cursor1;
        Texture2D horizontalLine;
        Texture2D verticalLine;
        Vector2 initialPos = Vector2.Zero;
        SpriteFont debugFont;

        int x, y, width, height;

        public Calibration(SpriteBatch sb, ContentManager cm)
            : base(cm, sb)
        {

        }
        public override void LoadContent()
        {
            horizontalLine = content.Load<Texture2D>("horizontal_line");
            verticalLine = content.Load<Texture2D>("vertical_line");
            debugFont = content.Load<SpriteFont>("MessageBox\\DefaultText");
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (Input.KeyPressed(Keys.Escape))
                GlobalContainer.gameStateManager.SetCurrentState(GameState.Menu);

            if (Input.KeyPressed(Keys.Enter))
                CalibrationCompleted();

            cursor1 = Input.GetCursor(Input.Cursors.Cursor1);

            if (cursor1.Visible && !calibrationStarted)
            {
                calibrationStarted = true;
                initialPos = cursor1.Position;
                y = (int)initialPos.Y;
                x = (int)initialPos.X;
                height = (int)initialPos.Y;
                width = (int)initialPos.X;

            }


            if (cursor1.Position.Y < y && cursor1.Position.Y > 0)
                y = (int)cursor1.Position.Y;

            if (cursor1.Position.Y > height && cursor1.Position.Y < Constants.SCREEN_HEIGHT)
                height = (int)cursor1.Position.Y;

            if (cursor1.Position.X < x && cursor1.Position.X > 0)
                x = (int)cursor1.Position.X;

            if (cursor1.Position.X > width && cursor1.Position.Y < Constants.SCREEN_WIDTH)
                width = (int)cursor1.Position.X;

        }

        private void CalibrationCompleted()
        {
            float offsetX = 0.1f * width;
            float offsetY = 0.1f * height;

            Input.SetCalibration(new Rectangle((int)(x + offsetX), (int)(y + offsetY), (int)(width - offsetX), (int)(height - offsetY)));
            GlobalContainer.gameStateManager.SetCurrentState(GameState.Menu);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(horizontalLine, new Vector2(0, y), Color.Red);
            spriteBatch.Draw(horizontalLine, new Vector2(0, height), Color.Blue);
            spriteBatch.Draw(verticalLine, new Vector2(x, 0), Color.Green);
            spriteBatch.Draw(verticalLine, new Vector2(width, 0), Color.Yellow);
            spriteBatch.DrawString(debugFont, "Size: " + Input.Cursor1Size(), Vector2.Zero, Color.White);
            spriteBatch.End();
        }


    }
}
